package com.eric.r2d2.urlbase;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import com.alibaba.fastjson.JSONObject;

public class Prioritizer {
	static int defaul_priority = 4;

	public static void main(String[] args) throws IOException {
		if (args.length < 2) {
			System.out.println("Usage:Prioritize Url_Base_File Dir_Seed_files");
			System.exit(1);
		}
		
		//URL
		File url_file = new File(args[0]);
		BufferedReader br_url = null;
	
		//定义seed files
		File dir_seeds = new File(args[1]);
		File[] seed_files = dir_seeds.listFiles();
		Map<Integer,HashSet<String>> seed_map = readSeed(seed_files);
		
//		BlockingQueue<JSONObject> queue = new LinkedBlockingQueue<JSONObject>();
		BlockingQueue<JSONObject> priotized_queue = new LinkedBlockingQueue<JSONObject>();
		
		try {
			 br_url = new BufferedReader(new FileReader(url_file));
//			 br_seed = new BufferedReader(new FileReader(seed_file));
			 
				String line;
			// 将目标url文件读取到queue里
				while ((line = br_url.readLine()) != null) {
//					queue.add(JSON.parseObject(line));
					priotized_queue.add((PrioritizeByDomain(JSONObject.parseObject(line),seed_map)));
				}
				br_url.close();
				
				line = null;

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		url_file.delete();
		BufferedWriter bw;
		try {
			bw = new BufferedWriter(new FileWriter(url_file, true));
			while (!priotized_queue.isEmpty()) {
				bw.write(priotized_queue.poll().toJSONString());
				bw.write("\n");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static Map<Integer,HashSet<String>> readSeed (File[] seed_files) throws IOException {
		Map<Integer,HashSet<String>> map = new HashMap<>();
		HashSet<String> seed_set = null;
		BufferedReader br = null;
		
		//依次读取10个seed file
		for (File file : seed_files) {
			int priority = Integer.parseInt(file.getName().split("\\.")[1]);
			seed_set = new HashSet<String>();
			br = new BufferedReader(new FileReader(file));
			String line;
			while ((line = br.readLine()) != null) {
				seed_set.add(line);
			}
			map.put(priority, seed_set);
		}
		return map;
	}
	
	/*根据seed file的后缀（1到10）判断优先级，1即优先级1
	 * 返回一个添加了优先级的新队列
	 * seed file记录的是一个个根域名
	*/
	public static JSONObject PrioritizeByDomain (JSONObject json,Map<Integer,HashSet<String>> seed_files_map) {
		boolean bingo = false;
		
		for (int i : seed_files_map.keySet()){
			
			try {
				if (seed_files_map.get(i).contains(GetRoot.getRoot(json.getString("url")))) {
					json.put("priority", i);
					bingo = true;
					break;
				}
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
				
		//如果都没有匹配，则默认priority
		if (!bingo) json.put("priority", defaul_priority);

		return json;
	}
}
